package com.qqtop10.manager.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtop10.manager.model.Points;
import com.qqtop10.manager.service.PointsService;
import com.qqtop10.manager.utils.Result;

/**
 * 支付点数配置控制器
 * 
 * @author Jovi
 *
 */
@RequestMapping("/manager/points/")
@Controller
public class PointsController extends BaseController {

	@Autowired
	private PointsService pointsService;

	/**
	 * 获取支付扣除的点数
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("getPoints.do")
	@ResponseBody
	public Result getPoints(HttpServletRequest request,
			HttpServletResponse response) {
		Result result = new Result();
		result.setCode(1);
		result.setMsg("获取支付点数成功");
		result.setData(pointsService.getPoints());
		
		return result;
	}

	/**
	 * 更新支付扣除的点数
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("updatePoints.do")
	@ResponseBody
	public Result updatePoints(HttpServletRequest request,
			HttpServletResponse response) {
			Result result = new Result();
			try {
			String password = request.getParameter("password");
			//System.out.println(password);
			if (!super.getLogind().getPassword()
					.equals(super.md5(password).toLowerCase())) {
				result.setCode(99);
				//result.setMsg("设置支付点数失败：管理员密码错误");
				return result;
			}
			Points points = new Points(
					Float.parseFloat((request.getParameter("points") == null || "".equals(request.getParameter("points"))) ? "0" : request.getParameter("points")),
					Float.parseFloat((request.getParameter("integralpro") == null || "".equals(request.getParameter("integralpro"))) ? "0" : request.getParameter("integralpro")),
//					Float.parseFloat(request.getParameter("integralpro")),
					Float.parseFloat((request.getParameter("balances") == null || "".equals(request.getParameter("balances"))) ? "0" : request.getParameter("balances")),
//					Float.parseFloat(request.getParameter("balances")),
					Float.parseFloat(request.getParameter("backcover")),
					Float.parseFloat(request.getParameter("poundage")),
					Float.parseFloat(request.getParameter("lowerlimit")),
					Float.parseFloat(request.getParameter("toplimit"))
					);
			int i = pointsService.updatePoints(points);
			if (i > 0) {
				result.setCode(1);
				//result.setMsg("设置支付点数成功");
			} else {
				result.setCode(98);
				//result.setMsg("设置支付点数失败：可能参数错误");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(500);
			result.setMsg("设置支付点数失败：" + e.getMessage());
		}

		return result;
	}
	/*
	 * 获取 积分比例
	 * 
	@RequestMapping("getIntegral.do")
	@ResponseBody
	public Result getIntegral(HttpServletRequest request,HttpServletResponse response) {
		Result result = new Result();
		result.setCode(1);
		result.setMsg("获取支付点数成功");
		result.setData(pointsService.getIntegral());
		return result;
	}*/
	

}
